package com.study.mike.spring.sample.create;

import com.study.mike.spring.service.CombatService;
import com.study.mike.spring.service.LoveService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CreateBeanXmlTest {

    private ApplicationContext context;

    @Before
    public void prepare() {
        context = new ClassPathXmlApplicationContext("com/study/mike/spring/sample/create/application.xml");
    }

    @Test
    public void constructor() {
        BeanXmlTest beanXmlTest = context.getBean(BeanXmlTest.class);
        System.err.println(beanXmlTest);
//        LoveServiceFactory abean = (LoveServiceFactory) context.getBean("loveServiceFactory");
//        System.err.println(abean.getCombatServiceFromFactoryMethod(60));
    }

    @Test
    public void staticFactoryMethod() {
        LoveService ls = (LoveService) context.getBean("loveService");
        ls.doLove();
    }

    @Test
    public void factoryMethod() {
        CombatService cs = context.getBean(CombatService.class);
        cs.doInit();
        cs.combating();
    }

    @Test
    public void factoryBean() {
		System.err.println("获取 bean ---");
    	LoveService ls = (LoveService) context.getBean("loveService2");
        ls.doLove();

		System.err.println("获取工厂 factoryBean 自己 ---");
        LoveServiceFactoryBean lsfb = (LoveServiceFactoryBean) context.getBean("&loveService2");
        System.out.println(lsfb);
    }

    @Test
    public void alias() {
        CombatService cs = (CombatService) context.getBean("combat");
        cs.doInit();
        cs.combating();
    }

}
